package tt.dz.mvc.controller.middleware;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.exception.BizException;
import tt.dz.mvc.controller.base.BaseController;
import tt.dz.service.imp.middleware.QueryService;
import tt.dz.service.imp.middleware.QueryServiceV1;
import tt.dz.service.imp.middleware.vo.RateVO;
import tt.dz.tcp.socket.exception.CreateConnectionFailException;
import tt.dz.tcp.socket.exception.DuplicateInstructionException;
import tt.dz.tcp.socket.exception.RetryFailException;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Created by luoyp on 2016/6/17.
 */
@Controller
public class QueryController extends BaseController
{
	private Log log = LogFactory.getLog(QueryController.class);

	@Autowired
	QueryService queryService;

	@Autowired
	QueryServiceV1 queryServiceV1;

	@RequestMapping("/retrieveState")
	@ResponseBody
	public JsonCommon<String> retrieveState(String chargerId) throws CreateConnectionFailException, DuplicateInstructionException, RetryFailException, InterruptedException, TimeoutException
	{
		String state = queryService.retrieveState(chargerId);
		return new JsonCommon(state, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveChargingFee")
	@ResponseBody
	public JsonCommon<String> retrieveChargingFee(String chargerId) throws CreateConnectionFailException, DuplicateInstructionException, RetryFailException, InterruptedException, TimeoutException
	{
		QueryService.FeeByPeriod fee = queryService.retrieveChargingFee(chargerId);
		return new JsonCommon(fee, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveServiceFee")
	@ResponseBody
	public JsonCommon<String> retrieveServiceFee(String chargerId) throws CreateConnectionFailException, DuplicateInstructionException, RetryFailException, InterruptedException, TimeoutException
	{
		QueryService.FeeByPeriod fee = queryService.retrieveServiceFee(chargerId);
		return new JsonCommon(fee, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveWhiteLists")
	@ResponseBody
	public JsonForList<String> retrieveWhiteLists(String chargerId) throws InterruptedException, CreateConnectionFailException, TimeoutException, RetryFailException, DuplicateInstructionException
	{
		List<String> whiteLists = queryService.retrieveWhiteLists(chargerId);
		return new JsonForList(whiteLists, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveQrCode")
	@ResponseBody
	public JsonCommon<String> retrieveQrCode(String chargerId) throws CreateConnectionFailException, DuplicateInstructionException, RetryFailException, InterruptedException, TimeoutException
	{
		String qrcode = queryService.retrieveQrcode(chargerId);
		return new JsonCommon(qrcode, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveSerialNo")
	@ResponseBody
	public JsonCommon<String> retrieveSerialNo(String chargerId) throws CreateConnectionFailException, DuplicateInstructionException, RetryFailException, InterruptedException, TimeoutException
	{
		String no = queryService.retrieveSerialNo(chargerId);
		return new JsonCommon(no, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveUserCard")
	@ResponseBody
	public JsonCommon<String> retrieveUserCard(String chargerId) throws CreateConnectionFailException, DuplicateInstructionException, RetryFailException, InterruptedException, TimeoutException
	{
		String card = queryService.retrieveUserCard(chargerId);
		return new JsonCommon(card, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping("/retrieveFeeV1")
	@ResponseBody
	public JsonCommon<String> retrieveFeeV1(String chargerId) throws CreateConnectionFailException, TimeoutException, InterruptedException, RetryFailException, BizException
	{
		String fee = queryServiceV1.retrieveFee(chargerId);
		if (fee != null)
		{
			String feeRate1 = fee.substring(50, 54);
			String feeRate2 = fee.substring(54, 58);
			String feeRate3 = fee.substring(58, 62);
			String feeRate4 = fee.substring(62, 66);
			String server_fee = fee.substring(66, 70);
			String timeRate = fee.substring(70, 166);

			float feeRate1_float = StringUtil.float16to10ByParamDesc(feeRate1, 100);
			float feeRate2_float = StringUtil.float16to10ByParamDesc(feeRate2, 100);
			float feeRate3_float = StringUtil.float16to10ByParamDesc(feeRate3, 100);
			float feeRate4_float = StringUtil.float16to10ByParamDesc(feeRate4, 100);
			float server_fee_float = StringUtil.float16to10ByParamDesc(server_fee, 100);

			RateVO vo = new RateVO();
			vo.setRate1(feeRate1_float);
			vo.setRate2(feeRate2_float);
			vo.setRate3(feeRate3_float);
			vo.setRate4(feeRate4_float);
			vo.setServerFee(server_fee_float);

			List<String> feePeriod = new ArrayList<>();

			String fee_id = null;

			ArrayList<Integer> list_index = new ArrayList<>();
			ArrayList<String> list_fee = new ArrayList<>();
			for (int i = 0; i < timeRate.length(); i += 2)
			{
				String t = timeRate.substring(i, i + 2);
				if (fee_id == null || !t.equals(fee_id))
				{
					fee_id = t;
					list_fee.add(fee_id);
					list_index.add(i);
				}
			}

			if(list_index.isEmpty() || list_index.size() > 4)
				throw new BizException("Period size of charging fee is larger than 4");

			for (int j = 0; j < list_index.size(); j++)
			{
				Integer i = list_index.get(j);
				String s_time = String.valueOf(i / 4) + ":" + (i % 4 == 2 ? "30" : "00");
				String f = list_fee.get(j);
				String e_time = "23:59";
				if (j + 1 < list_index.size())
					e_time = String.valueOf(list_index.get(j + 1) / 4) + ":" + (list_index.get(j + 1) % 4 == 2 ? "30" : "00");

				feePeriod.add(String.format("%1$s - %2$s的%3$s号费率为", s_time, e_time, f));
			}
			vo.setFeePeriod(feePeriod);

			return new JsonCommon(vo, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
		}
		else
			return new JsonCommon("400", "Fail");
	}

}
